package com.emellend.api.layoutanimation;

import java.util.List;
import com.emellend.api.R;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
/**
 * gridview的每个条目按从下到上，从右往左的顺序一一显示
 * @author emellend
 *
 */
public class LayoutAnimation5 extends Activity {
	private List<ResolveInfo> mApps;
	private PackageManager pm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadApps();
		setContentView(R.layout.layout_animation_5);
		GridView grid = (GridView) this.findViewById(R.id.myGrid);
		grid.setAdapter(new AppsAdapter());
	}

	private void loadApps() {
		Intent intent=new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		pm = getPackageManager();
		mApps=pm.queryIntentActivities(intent, 0);
	}
	
	public class AppsAdapter extends BaseAdapter{

		public int getCount() {
			return Math.min(32, mApps.size());
		}

		public Object getItem(int position) {
			return mApps.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView==null) {
				imageView=new ImageView(LayoutAnimation5.this);
				imageView.setScaleType(ScaleType.FIT_CENTER);
				int w = Math.round(getResources().getDisplayMetrics().density*36);
				imageView.setLayoutParams(new GridView.LayoutParams(w,w));
			}else {
				imageView=(ImageView) convertView;
			}
			ResolveInfo resolveInfo = mApps.get(position);
			imageView.setImageDrawable(resolveInfo.loadIcon(pm));
			return imageView;
		}
		
	}
}
